package com.example.myapplication.mode;
import com.example.myapplication.bean.weatherResult;
import com.example.myapplication.iface.weatherInterface;
import com.example.myapplication.listener.weatherlistener;
import com.example.myapplication.service.weatherservice;

/**
 * Created by renhao on 2017/3/21.
 */

public class weathermode implements WeatherInterface{
        private  static final  String BASE_URL="http://www.weather.com.cn/data

                / ";
        private WeatherService service;
        private Retrofit retrofit;
        @Override
        public void getWeather(String cityNumber, final WeatherListener listener) {
            //步骤一：创建retrofit对象
            retrofit=new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            //步骤二：创建API请求

            service=retrofit.create(WeatherService.class);
            Call<WeatherResult> call=service.getResult(cityNumber);

            //步骤三：发送请求并处理结果

            call.enqueue(new Callback<WeatherResult>() {
                @Override
                public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                    if (response.isSuccessful() && response.body()!=null){
                        listener.onResponse(response.body().getWeatherinfo());
                    }
                }

                @Override
                public void onFailure(Call call, Throwable t) {
                    listener.onFailure(t.toString());

                }
            });


